package com.caigou.modules.tools.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.caigou.comm.base.constants.RouterPath
import com.caigou.comm.base.container.BaseFragment
import com.caigou.comm.base.widget.SpaceItemDecoration
import com.caigou.modules.tools.adapter.TaskListAdapter
import com.caigou.modules.tools.bean.ActiveItemBean
import com.caigou.modules.tools.databinding.FragmentTaskListBinding
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/11/03
 */
@AndroidEntryPoint
class TodayTaskListFragment : BaseFragment() {

    private var _binding: FragmentTaskListBinding? = null
    private val binding get() = _binding!!

    /**
     * RecyclerView Divider
     */
    private val recyclerViewItemDecoration by lazy {
        SpaceItemDecoration(requireActivity())
    }

    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(requireActivity())
    }

    private val mAdapter: TaskListAdapter by lazy {
        TaskListAdapter(mutableListOf())
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentTaskListBinding.inflate(inflater, container, false)
        return _binding?.root
    }

    override fun initView() {
        binding.activeTaskListRecyclerView.run {
            layoutManager = linearLayoutManager
            adapter = mAdapter
            itemAnimator = DefaultItemAnimator()
            addItemDecoration(recyclerViewItemDecoration)
        }

        mAdapter.setOnItemClickListener { _, _, position ->
            val item = mAdapter.getItem(position)
            if (item.status == 0) {
                ARouter.getInstance().build(RouterPath.Forum.PAGE_FORUM).navigation()
            }
        }
    }

    override fun lazyLoad() {
    }

    override fun onBack() {
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    fun setTaskList(list: MutableList<ActiveItemBean>) {
        mAdapter.setList(list)
    }
}